package com.augurit.common.workdayholiday.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.dao.WhSpecialTimeDao;
import com.augurit.common.entity.WhSpecialTime;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.common.workdayholiday.convert.WhSpecialTimeConvertor;
import com.augurit.common.workdayholiday.model.TimeSpan;
import com.augurit.common.workdayholiday.service.IWhSpecialTimeService;
import com.augurit.common.workdayholiday.web.form.WhSpecialTimeForm;

@Service
@Transactional
public class WhSpecialTimeServiceImpl implements IWhSpecialTimeService{

	@Autowired
	private WhSpecialTimeDao whSpecialTimeDao;
	
	/**
	 * 根据主键获取Form对象
	 */
	@Transactional(readOnly = true)
	public WhSpecialTimeForm get(Long id) {
		WhSpecialTime entity = whSpecialTimeDao.get(id);
		return WhSpecialTimeConvertor.convertVoToForm(entity);
	}
	
	/**
	 * 删除Form对象列表
	 */
	public void delete(Long... ids) {
		whSpecialTimeDao.delete(ids);
	}

	/**
	 * 保存新增或修改的Form对象列表
	 */
	public void save(WhSpecialTimeForm... forms) {
		if(forms != null)
			for(WhSpecialTimeForm form : forms)
				this.save(form);
	}
	
	/**
	 * 保存新增或修改的Form对象
	 */
	public void save(WhSpecialTimeForm form){
		
		if(form != null){
			WhSpecialTime entity = null;
			
			//准备VO对象
			if(form != null && form.getId() != null){
				entity = whSpecialTimeDao.get(form.getId());
			}else{
				entity = new WhSpecialTime();
			}
			
			//属性值转换
			WhSpecialTimeConvertor.convertFormToVo(form, entity);
			
			//保存
			whSpecialTimeDao.save(entity);
			
			//回填ID属性值
			form.setId(entity.getId());
		}
	}

	/**
	 * 根据Form对象的查询条件作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<WhSpecialTimeForm> search(Page<WhSpecialTimeForm> page, WhSpecialTimeForm form) {
		// 建立新的分页对象
		Page pg = PageUtils.newPageInstance(page);
		
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("from WhSpecialTime ps where 1=1");
		List values = new ArrayList();
		
		// 查询条件
		if(form != null){
			
		}
		
		//排序
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// 执行分页查询操作
		whSpecialTimeDao.findPage(pg, hql.toString(), values);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<WhSpecialTimeForm> list = WhSpecialTimeConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 根据过滤条件列表作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<WhSpecialTimeForm> search(Page<WhSpecialTimeForm> page, List<PropertyFilter> filters) {
		// 建立新的分页对象
		Page<WhSpecialTime> pg = PageUtils.newPageInstance(page);
		
		// 按过滤条件分页查找对象
		whSpecialTimeDao.findPage(pg, filters);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<WhSpecialTimeForm> list = WhSpecialTimeConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	
	
	/**
	 * 获取所有的自定义假期（包括元旦、清明等传统节日及非工作日时间中自定义的假期，但不包括周末）
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<String> getCustomHolidays(){
		List<String> holidays = null;
		String hql = "from WhSpecialTime ps where ps.isHoliday = ?";
		List values = new ArrayList();
		values.add("Y");
		List<WhSpecialTime>  enities = whSpecialTimeDao.find(hql, values);
		if(enities != null && enities.size() > 0){
			holidays = new ArrayList<String>();
			for(WhSpecialTime entity:enities){
				if(WhSpecialTimeConvertor.isAllDay(entity)){
					holidays.add(DateUtils.dateTimeToString(entity.getDay(),"dd/MM/yyyy"));
				}
			}
		}
		return holidays;
	}
	
	/**
	 * 获取所有的自定义假期（包括元旦、清明等传统节日及非工作日时间中自定义的假期，但不包括周末）
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<WhSpecialTime> getCustomHolidaysList(){
		String hql = "from WhSpecialTime ps where ps.isHoliday = ?";
		List values = new ArrayList();
		values.add("Y");
		List<WhSpecialTime>  enities = whSpecialTimeDao.find(hql, values);
		return enities;
	}
	
	/**
	 * 获取所有的自定义工作日，就是那些本该为周末或者传统节日的工作日
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<String> getCustomWorkdays(){
		List<String> custormWorkdays = null;
		String hql = "from WhSpecialTime ps where ps.isHoliday = ?";
		List values = new ArrayList();
		values.add("N");
		List<WhSpecialTime>  enities = whSpecialTimeDao.find(hql, values);
		if(enities != null && enities.size() > 0){
			custormWorkdays = new ArrayList<String>();
			for(WhSpecialTime entity:enities){
				//整天
				if(WhSpecialTimeConvertor.isAllDay(entity)){
					custormWorkdays.add(DateUtils.dateTimeToString(entity.getDay(),"dd/MM/yyyy"));
				}
			}
		}
		return custormWorkdays;
	}
	
	/**
	 * 获取所有的自定义工作日，就是那些本该为周末或者传统节日的工作日
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<WhSpecialTime> getCustomWorkdaysList(){
		String hql = "from WhSpecialTime ps where ps.isHoliday = ?";
		List values = new ArrayList();
		values.add("N");
		List<WhSpecialTime> enities = whSpecialTimeDao.find(hql, values);
		return enities;
	}
	
	/**
	 * 获取中所有的自定义工作日对应的时长
	 * @return
	 */
	public Map<String,TimeSpan> getCustomWorkdaysTimeSpan(){
		Map<String,TimeSpan> map = new HashMap<String,TimeSpan>();
		
		return null;
	}
	
	public void addSpecialDay(final Date day, String isHoliday, String memo){
		WhSpecialTime entity = new WhSpecialTime();
		entity.setDay(day);
		entity.setIsHoliday(isHoliday);
		entity.setIsAllDay("Y");
		whSpecialTimeDao.save(entity);
	}
	
	/**
	 * 添加非常规工作日时间段
	 * @param from
	 * @param to
	 * @param isHoliday
	 * @param memo
	 */
	public void addSpecialPeriod(final Date from, final Date to, String isHoliday, String memo){
		assert(to.compareTo(from) >= 0);
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(from);
		
		Date day = from;
		while(day.compareTo(to) <= 0){
			addSpecialDay(day, isHoliday, memo);
			calendar.add(Calendar.DAY_OF_MONTH, 1);
			day = calendar.getTime();
		};
	}
}
